package com.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.alibaba.fastjson.JSON;
import com.dao.WellStudentDao;
import com.opensymphony.xwork2.ActionContext;
import com.po.Well_StudentPO;
@Controller
public class WellStudentAction {
	@Autowired
	private WellStudentDao wd;
	private Well_StudentPO ws;
	private List<Well_StudentPO> listWellStudentPO;
	private File image;
	private String imageFileName;
	
	
	/**
	 * 展示所有优秀学员的信息
	 * @return
	 */
	public String show(){
		listWellStudentPO  = wd.findWellStudentInfo();
		return "showInfo";
	}
	/**
	 * 上传图片
	 */
	public String upImage(){
		try {
			//给上传的图片重新起名字
			String name = imageFileName;
			long date = System.currentTimeMillis();
			String NewName = date + name.substring(name.lastIndexOf(".")); 
			
			//获取ServletContext对象
			ServletContext context = ServletActionContext.getServletContext();
			
			//	给服务器中上传文件
			FileInputStream fis = new FileInputStream(image);
			int t = 0;
			byte[] bys = new byte[1024];
			FileOutputStream fos = new FileOutputStream(context.getRealPath("/") + "person_image/" + NewName);
			while((t = fis.read(bys)) != -1){
				fos.write(bys, 0, t);
			}
			//关闭流
			fis.close();
			fos.close();
			//服务器响应的数据
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("text/html;charset=utf-8");
			response.getWriter().write(NewName);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	 return null;
	}
	
	/**
	 * 跳转到添加页面
	 */
	public String preSaveInfo(){
		return "preSaveInfo";
	}
	/**
	 * 添加新的学员信息
	 * @return
	 */
	public String saveInfo(){
		HttpServletResponse response = ServletActionContext.getResponse();
		HashMap<String, String> map = new HashMap<String, String>();
		try {
			wd.saveInfo(ws);
			map.put("code", "EC001");
		} catch (Exception e) {
			map.put("code", "EC002");
		}
		
		try {
			String msg = JSON.toJSONString(map);
			response.setContentType("text/html;charset=utf-8");
			response.getWriter().write(msg);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 删除学员信息
	 * @return
	 */
	public String delete(){
		HttpServletRequest request = ServletActionContext.getRequest();
		 String id = request.getParameter("id");
		 wd.deleteInfo(id);
		return "delete";
	}
	/**
	 * 跳转到修改学员信息界面
	 * @return
	 */
	public String preUpdate(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("id");
		ActionContext.getContext().put("id", id);
		return "preUpdate";
	}
	/**
	 * 修改学员信息
	 * @return
	 */
	public String update(){
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		HashMap<String, String> map = new HashMap<String, String>();
		try {
			int id = Integer.parseInt(request.getParameter("id"));
			
			ws.setId(id);
			wd.updateInfo(ws);
			map.put("code", "EC001");
		} catch (Exception e) {
			map.put("code", "EC002");
		}
		
		try {
			String msg = JSON.toJSONString(map);
			response.setContentType("text/html;charset=utf-8");
			response.getWriter().write(msg);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	
	
	public File getImage() {
		return image;
	}
	public void setImage(File image) {
		this.image = image;
	}
	public String getImageFileName() {
		return imageFileName;
	}
	public void setImageFileName(String imageFileName) {
		this.imageFileName = imageFileName;
	}
	public List<Well_StudentPO> getListWellStudentPO() {
		return listWellStudentPO;
	}
	public void setListWellStudentPO(List<Well_StudentPO> listWellStudentPO) {
		this.listWellStudentPO = listWellStudentPO;
	}
	
	
	
}
